package member.service.impl;

import java.io.IOException;
import java.net.InetAddress;
import java.util.List;

import javax.annotation.Resource;

import member.service.IMemberService;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import common.dao.MemberDAO;
import common.dao.MemberInfoDAO;
import common.dao.MemberOtherInfoDAO;
import common.pojo.Member;
import common.pojo.MemberInfo;
import common.pojo.MemberOtherInfo;


/** 
 * @author pfp
 * @version 
 * 类说明 
 */

@Scope("prototype")
@Service
public  class MemberService implements IMemberService {
	@Resource
	private MemberOtherInfoDAO memberOtherInfoDao;
	@Resource
	private MemberInfoDAO memberInfoDAO;
	@Resource
	private MemberDAO memberDAO;


	
	public List<Member> getMemberListByPage(int start, int limit,
			String orderby, String desc) {
		return memberDAO.getListByPage(start, limit, orderby, desc);
	}
	
	public int getCount(){
		return memberDAO.getCount();
	}
	
	public boolean delete(String ids) {
		String[] id = StringUtils.split(ids, ",");
		for(int i=0;i<id.length;i++){
			memberDAO.delete(Integer.valueOf(id[i]));
			memberOtherInfoDao.delete(Integer.valueOf(id[i]));
			memberInfoDAO.delete(Integer.valueOf(id[i]));
		}
		return true;
	}

	public Member findMemberById(int id) {
		return memberDAO.findInfoById(id);
	}

	public MemberInfo findInfoById(int id) {
		return memberInfoDAO.findInfoById(id);
	}

	public MemberOtherInfo findOtherInfoById(int id) {
		return memberOtherInfoDao.findInfoById(id);
	}
	
	public boolean update(Member member, MemberInfo memberInfo, String repwd) {
		memberDAO.update(member, repwd);
		memberInfoDAO.update(memberInfo);
		return true;
	}

	public boolean save(Member member, MemberInfo memberInfo,
			MemberOtherInfo memberOtherInfo) throws IOException  {
		int mid = memberDAO.save(member);
		memberInfo.setMemberId(mid);
		memberInfoDAO.save(memberInfo);
		
		memberOtherInfo.setMemberId(mid);
		
		InetAddress addr = InetAddress.getLocalHost();
		String ip = addr.getHostAddress().toString();
		memberOtherInfo.setRegIp(ip);
		memberOtherInfoDao.saveback(memberOtherInfo);
		return true;
	}

	public Member findByName(String name) {
		// TODO Auto-generated method stub
		return memberDAO.findInfoByName(name);
	}

	public boolean update(String ip, Integer id) {
		// TODO Auto-generated method stub
		return memberOtherInfoDao.update(ip,id);
	}

	public void update(Member member, MemberInfo memberInfo) {
		// TODO Auto-generated method stub
		memberDAO.update(member);
		memberInfoDAO.update(memberInfo);
	}

	public void updatePwd(String pwd, int memberId) {
		// TODO Auto-generated method stub
		memberDAO.update(pwd,memberId);
	}

	public int save(Member member, MemberOtherInfo other) {
		 member.setState(true);
		int memberId=memberDAO.save(member);
		memberInfoDAO.save(memberId);
		other.setMemberId(memberId);
		memberOtherInfoDao.save(other);
		return memberId;
	}

}